﻿
namespace MyFurion.Application.Controller
{
    /// <summary>
    /// 字典
    /// </summary>
    public class DictController:IDynamicApiController
    {
        private readonly DictRepository _repository;
        private readonly DictDetailRepository _dictDetailRepository;
        public DictController(DictRepository repository, DictDetailRepository dictDetailRepository)
        {
            _repository = repository;
            _dictDetailRepository = dictDetailRepository;
        }
        /// <summary>
        /// 新增字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("add")]
        public async Task Add(DictInfoAdd input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool isExists = await _repository.DataExists(input.DictCode);
            if (isExists)
            {
                throw Oops.Oh(ErrorMsgConst.ExistError);
            }
            var model = input.Adapt<DictInfo>();
            bool result=await _repository.Add(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 修改字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task Update(DictInfoUpdate input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool isExists = await _repository.DataExists(input.DictCode,input.Id);
            if (isExists)
            {
                throw Oops.Oh(ErrorMsgConst.ExistError);
            }
            var model =await _repository.GetEntityById(input.Id);
            if (model == null)
            {
                throw Oops.Oh(ErrorMsgConst.NoRecord);
            }
            model.DictCode = input.DictCode;
            model.DictName = input.DictName;
            model.Status= input.Status;
            bool result = await _repository.Update(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 新增字典值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("addDictItem")]
        public async Task AddDictItem(DictDetailInfoAdd input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool isExists = await _dictDetailRepository.DataExists(input.DictId,input.DictItemCode);
            if (isExists)
            {
                throw Oops.Oh(ErrorMsgConst.ExistError);
            }
            var model = input.Adapt<DictDetailInfo>();
            bool result = await _dictDetailRepository.Add(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 修改字典值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("updateDictItem")]
        public async Task UpdateDictItem(DictDetailInfoUpdate input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool isExists = await _dictDetailRepository.DataExists(input.DictId,input.DictItemCode, input.Id);
            if (isExists)
            {
                throw Oops.Oh(ErrorMsgConst.ExistError);
            }
            var model = await _dictDetailRepository.GetEntityById(input.Id);
            if (model == null)
            {
                throw Oops.Oh(ErrorMsgConst.NoRecord);
            }
            model.DictItemCode = input.DictItemCode;
            model.DictItemName = input.DictItemName;
            model.Status = input.Status;
            model.DictId= input.DictId;
            bool result = await _dictDetailRepository.Update(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("delete")]
        public async Task Delete([FromQuery] BaseIdInput input)
        {
            bool result = await _repository.DeleteById(input.Id);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
            else
            {
                //删除字典值
                await _dictDetailRepository.DeleteByWhere(it=>it.DictId==input.Id);
            }
        }
        /// <summary>
        /// 批量删除字典
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("batchDelete")]
        public async Task BatchDelete([FromQuery]BaseIdsInput input)
        {
            bool result = await _repository.DeleteByWhere(it=>input.Ids.Contains(it.Id));
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
            else
            {
                //删除字典值
                await _dictDetailRepository.DeleteByWhere(it =>input.Ids.Contains(it.DictId));
            }
        }
        /// <summary>
        /// 删除字典值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("deleteDictItem")]
        public async Task DeleteDictItem([FromQuery] BaseIdInput input)
        {
            bool result = await _dictDetailRepository.DeleteById(input.Id);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
        }
        /// <summary>
        /// 批量删除字典值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("batchDeleteItem")]
        public async Task BatchDeleteItem([FromQuery] BaseIdsInput input)
        {
            bool result = await _dictDetailRepository.DeleteByWhere(it => input.Ids.Contains(it.Id));
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
        }
        /// <summary>
        /// 修改字典状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("setStatus")]
        public async Task ModifyStatus(SetStatusInfo input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool result = await _repository.Update(it => it.Id == input.Id, it => new DictInfo() { Status = input.Status });
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.StatusError);
            }
            else
            {
                //字典值状态修改
                if (input.Status == EnumStatus.Disabled)
                {
                    await _dictDetailRepository.Update(it => it.DictId == input.Id&&it.Status==EnumStatus.Enabled, it => new DictDetailInfo() { Status = input.Status });
                }
                //else
                //{
                //    await _dictDetailRepository.Update(it => it.DictId == input.Id && it.Status == EnumStatus.Disabled, it => new DictDetailInfo() { Status = input.Status });
                //}
                
            }
        }
        /// <summary>
        /// 修改字典值状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("setIemStatus")]
        public async Task ModifyItemStatus(SetStatusInfo input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool result = await _dictDetailRepository.Update(it => it.Id == input.Id, it => new DictDetailInfo() { Status = input.Status });
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.StatusError);
            }
        }
        /// <summary>
        /// 获取字典分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("page")]
        public async Task<PageResult<DictInfo>>QueryPage(DictInfoPage input)
        {
            return await _repository.GetPageList(input);
        }
        /// <summary>
        /// 获取字典列表数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<List<DictInfo>> QueryList(DictInfoPage input)
        {
            return await _repository.GetDataList(input);
        }
        /// <summary>
        /// 根据id获取字典详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<DictInfo> GetDetail(long id)
        {
            return await _repository.GetEntityById(id);
        }
        /// <summary>
        /// 获取字典值分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("pageItem")]
        public async Task<PageResult<DictDetailInfo>> QueryItemPage(DictDetailInfoPage input)
        {
            return await _dictDetailRepository.GetPageList(input);
        }
        /// <summary>
        /// 获取字典值列表数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("listItem")]
        public async Task<List<DictDetailInfo>> QueryListItem(DictDetailInfoPage input)
        {
            return await _dictDetailRepository.GetDataList(input);
        }
        /// <summary>
        /// 根据id获取字典值详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detailItem")]
        public async Task<DictDetailInfo> GetItemDetail(long id)
        {
            return await _dictDetailRepository.GetEntityById(id);
        }
        /// <summary>
        /// 根据字典编码获取字典值列表
        /// </summary>
        /// <param name="dictCode"></param>
        /// <returns></returns>
        [HttpGet("itemByDictCode")]
        public async Task<List<DictDetailInfo>>GetItemByDictCode(string dictCode)
        {
            return await _dictDetailRepository.GetByDictCode(dictCode, EnumStatus.Enabled);
        }
    }
}
